import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.regex.*;

public class SimpleGUI extends JFrame {
	private JButton button1 = new JButton("Start");
	private JButton button2 = new JButton("Stop");
        private JButton button3 = new JButton("Clear");
	private JProgressBar progressbar = new JProgressBar();
        private JTextField input1 = new JTextField("", 5);
	private JLabel label1 = new JLabel("Input file:");
        private JTextField input2 = new JTextField("", 5);
	private JLabel label2 = new JLabel("Ouput file:");
        private JTextField input3 = new JTextField("", 5);
	private JLabel label3 = new JLabel("Regexp:");
        private JTextField input4 = new JTextField("", 5);
	private JLabel label4 = new JLabel("Replace:");

        private boolean stop = false;
//
	public SimpleGUI() {
                super("Regular Expressions 1.2");
		this.setBounds(400,300,250,200);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setResizable(false);

		Container container = this.getContentPane();
		container.setLayout(new GridLayout(6,2));
		container.add(label1);
		container.add(input1);
                container.add(label2);
		container.add(input2);
                container.add(label3);
		container.add(input3);
                container.add(label4);
		container.add(input4);
                
		button1.addActionListener(new Button1EventListener());
		container.add(button1);
		button2.addActionListener(new Button2EventListener());
		container.add(button2);
                button3.addActionListener(new Button3EventListener());
		container.add(button3);
                container.add(progressbar);
                progressbar.setStringPainted(true);
                
	}

        
	class Button2EventListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
                    stop = true;
		}	
	}
        
        class Button3EventListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
//                    strnum = 0;
                    progressbar.setValue(0);
                    input1.setText("");
                    input2.setText("");
                    input3.setText("");
                    input4.setText("");
		}	
	}
        
        
	class Button1EventListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Runnable r = new MyRunnable();
                        Thread t = new Thread(r);
                        t.start();
		}	
	}
        
        public class MyRunnable implements Runnable {
                public void run() {
                    
                        String fileName1 = input1.getText();
                        String fileName2 = input2.getText();
			FileWriter fw = null;
			BufferedWriter bw = null;
			FileReader fr = null;
			BufferedReader br = null;
			String pattern = input3.getText();
                        Pattern p = Pattern.compile(pattern);
                        Matcher m = p.matcher("");
                        String replacement = input4.getText();
                        
                        button3.setEnabled(false);
                        progressbar.setValue(0);
                        
                        try {
                                File file1 = new File(fileName1);
                                progressbar.setMaximum((int) file1.length()-1); // - end of file symbol

                                fr = new FileReader(fileName1);
                                br = new BufferedReader(fr);
				fw = new FileWriter(fileName2);
				bw = new BufferedWriter(fw);
                                String s = null;
                                                                                                                           
                                while ((s = br.readLine())!=null){
                                        if (stop){
                                            br.close();
                                            bw.close();
                                            stop = false;
                                            button3.setEnabled(true);
                                            return;
                                        }
                                        m.reset(s);
                                        s = m.replaceAll(replacement); 
                                        bw.write(s+"\r\n");
                                        progressbar.setValue(progressbar.getValue()+ s.length() + 2);
                                        Thread.sleep(100);
                                }
				br.close();
                                bw.close();
                                button3.setEnabled(true);

			} catch (Exception ex){
                                JOptionPane.showMessageDialog(null,"Please enter valid input and output file names!","Error message",JOptionPane.PLAIN_MESSAGE);
                                button3.setEnabled(true);
//                                ex.printStackTrace();                               
                        }
                }
        }

	public static void main(String[] args) {
		SimpleGUI app = new SimpleGUI();
		app.setVisible(true);
	}

}
